﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HROReporting.Infrastructure.Authentication;
using HROReporting.Infrastructure.CookieStorage;
using System.Web.Mvc;
using HROReporting.Services.Interfaces;
using HROReporting.Services.Messaging.DocentService;
using HROReporting.Controllers.ViewModels.Docent;
using HROReporting.Services.ViewModels;
using HROReporting.Controllers.ViewModels.DocentAccount;
using HROReporting.Services.Messaging.DocentIncheckingService;

namespace HROReporting.Controllers.Controllers
{
    public class DocentController : BaseController
    {
        private readonly IFormsAuthentication _formsAuthentication;
        private readonly IDocentService _docentService;
        private readonly IDocentIncheckingService _docentIncheckingService;

        public DocentController(ICookieStorageService cookieStorageService,
                                IFormsAuthentication formsAuthentication,
                                IDocentService docentService,
                                IDocentIncheckingService docentIncheckingService)
            : base (cookieStorageService)
        {
            _formsAuthentication = formsAuthentication;
            _docentService = docentService;
            _docentIncheckingService = docentIncheckingService;
        }

        // Lijst van docenten a.d.h.v. zoekveld
        public ActionResult Index()
        {
            DocentIndexView view = new DocentIndexView();
            view.ZoekTerm = null;
            view.Docent = null;
            view.Docenten = null;

            return View(view);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(DocentIndexView docentView)
        {
            DocentIndexView view = docentView;

            GetDocentByRequest request = new GetDocentByRequest();

            if (!string.IsNullOrWhiteSpace(docentView.ZoekTerm))
            {
                request.Achternaam = docentView.ZoekTerm;
                view.Docenten = _docentService.GetDocentenByAchternaam(request).Docenten;
            }
            if (view.Docenten != null && view.Docenten.Count() < 1 &&
                !string.IsNullOrWhiteSpace(docentView.ZoekTerm))
            {
                request.DocentCode = docentView.ZoekTerm;
                view.Docenten = _docentService.GetDocentenByDocentCode(request).Docenten;
            }
            if (view.Docenten != null && view.Docenten.Count() < 1 &&
                !string.IsNullOrWhiteSpace(docentView.ZoekTerm))
            {
                request.SchoolLocatie = docentView.ZoekTerm;
                view.Docenten = _docentService.GetDocentenBySchoolLocatie(request).Docenten;
            }

            return View(view);
        }

        public ActionResult List(string term)
        {
            DocentIndexView view = new DocentIndexView();

            GetDocentenByRequest request = new GetDocentenByRequest();
            request.Achternaam = term;
            List<string> results = null;

            if (results == null || results.Count() < 1)
            {
                results = _docentService.GetDocentenByAchternaamTerm(request).Termen;
            }
            if (results == null || results.Count() < 1)
            {
                request.DocentCode = term;
                results = _docentService.GetDocentenByDocentCodeTerm(request).Termen;
            }
            if (results == null || results.Count() < 1)
            {
                request.SchoolLocatie = term;
                results = _docentService.GetDocentenBySchoolLocatieTerm(request).Termen;
            }

            return Json(results.ToArray(), JsonRequestBehavior.AllowGet); 
        }

        // Docentgegevens a.d.h.v. een docentcode als parameter want die is uniek
        public ActionResult Details(string id)
        {
            DocentDetailView view = new DocentDetailView();

            GetDocentByRequest request = new GetDocentByRequest();
            request.DocentCode = id;

            GetDocentByResponse response = new GetDocentByResponse();
            response.Docent = _docentService.GetDocentByDocentCode(request).Docent;
            
            GetDocentIncheckingByRequest docentIncheckingRequest = new GetDocentIncheckingByRequest();
            docentIncheckingRequest.DocentCode = id;

            GetDocentIncheckingByResponse docentIncheckingResponse = _docentIncheckingService.GetDocentIncheckingenByDocentCode(docentIncheckingRequest);

            view.Docent = response.Docent;
            view.Docent.DocentIncheckingen = docentIncheckingResponse.DocentIncheckingen;

            return View(view);
        }

        public ActionResult MyAccount()
        {
            GetDocentByRequest docentRequest = new GetDocentByRequest();
            docentRequest.AuthenticationToken = _formsAuthentication.GetAuthenticationToken();

            GetDocentByResponse response = new GetDocentByResponse();
            response = _docentService.GetDocentByAuthenticationToken(docentRequest);

            DocentDetailView detailView = new DocentDetailView();
            detailView.Docent = response.Docent;

            return View(detailView);
        }
    }
}
